###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# Category.pm.pl -> Category module                                           #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Category;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";

use strict;

require "Output/Category.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = ("DB" => undef, "STATIC" => 0, @_);

  my $LANGUAGE = &Standard::GetLanguage("Category");

  #----------------------------------------------------------------------#
  # Checking fields...                                                   #

  unless ($SL::QUERY{'CID'}) {
    &Error::Error("SL", MESSAGE => $LANGUAGE->{'MISSING'}->{'CID'});
  }

  &Standard::ShowStatic(DB => $in{'DB'}, TYPE => "Category", ID => $SL::QUERY{'CID'}, PAGE => $SL::QUERY{'Page'}) unless ($in{'STATIC'});

  my $Category = $in{'DB'}->BinarySelect(
    TABLE   => "Categories",
    KEY     => $SL::QUERY{'CID'}
  );
  unless ($Category) {
    &Error::Error("SL", MESSAGE => $LANGUAGE->{'INVALID'}->{'CID'});
  }

  #----------------------------------------------------------------------#
  # Getting data...                                                      #

  $SL::QUERY{'Sort'}    ||= $GENERAL->{'CATEGORY_DEFAULT_SORT'};
  $SL::QUERY{'By'}      ||= $GENERAL->{'CATEGORY_DEFAULT_BY'};
  $SL::QUERY{'Page'}    ||= 1;
  $SL::QUERY{'PerPage'} ||= $GENERAL->{'CATEGORY_DEFAULT_PERPAGE'};

  my $TotalResources = $in{'DB'}->Query(
    TABLE   => "Resources",
    WHERE   => {
      CATEGORY  => [$SL::QUERY{'CID'}],
      STATUS    => ["1"]
    },
    MATCH   => "ALL",
    COUNT   => 1
  );
  
  my $Pages = &Standard::GetPages($SL::QUERY{'Page'}, $TotalResources, $SL::QUERY{'PerPage'});

  my ($Resources, $ResourcesIndex) = $in{'DB'}->Query(
    TABLE   => "Resources",
    WHERE   => {
      CATEGORY  => [$SL::QUERY{'CID'}],
      STATUS    => ["1"]
    },
    MATCH   => "ALL",
    SORT    => $SL::QUERY{'Sort'},
    BY      => $SL::QUERY{'By'},
    TO      => $Pages->{'start'}."-".$Pages->{'finish'}
  );

  @{ $Resources } = sort { $b->{'WEIGHTING'} <=> $a->{'WEIGHTING'} } @{ $Resources };

  my ($Categories, $CategoriesIndex) = $in{'DB'}->Query(
    TABLE   => "Categories",
    SORT    => "FULL_TITLE",
    BY      => "A-Z"
  );

  my %SubCategories;

  foreach my $category (@{ $Categories }) {
    if ($category->{'ROOT'} eq ($Category->{'ROOT'}.",".$Category->{'ID'})) {
      foreach my $key (keys %{ $category }) {
        $SubCategories{ $category->{'ID'} }->{$key} = $category->{$key};
      }
    } elsif ($category->{'ROOT'} =~ /^\Q$Category->{'ROOT'},$Category->{'ID'},\E(.*)$/ && $1) {
      my $root = $1;
      my @root = split(/,/, $root);
      if (scalar(@root) == 1) {
        push(@{ $SubCategories{$root[0]}->{'subcategories'} }, $category);
      }
      $SubCategories{$root[0]}->{'RESOURCES'} += $category->{'RESOURCES'};
    }
  }

  my @SubCategories;
  foreach my $key (keys %SubCategories) {
    push(@SubCategories, $SubCategories{$key});
  }

  @SubCategories = sort { $a->{'FULL_TITLE'} cmp $b->{'FULL_TITLE'} } @SubCategories;

  my %INPUT;

  $INPUT{'CATEGORY'}      = $Category;
  $INPUT{'RESOURCES'}     = $Resources;
  $INPUT{'SUBCATEGORIES'} = \@SubCategories;
  $INPUT{'PAGES'}         = $Pages;
  $INPUT{'CATEGORIES'}    = $Categories;
  $INPUT{'CATEGORIES_IX'} = $CategoriesIndex;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  &Standard::SetSkin(DB => $in{'DB'}, SKIN => $Category->{'SKIN'});

  my $Skin = Skin::Category->new();

  return $Skin->show(input => \%INPUT, static => 1) if ($in{'STATIC'});

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT);

  return 1;
}

1;